package com.ruoyi.common.utils.diagnosis;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysQuestion;
import com.ruoyi.common.core.domain.entity.SysQuestionRecord;

import java.util.List;

public class ScoreUtil {

    public static double getGrade(SysQuestion question) {
        if (question == null)
            return 0;
        String detail = question.getDetail();

        JSONObject jsonObject = JSONObject.parseObject(detail);
        String tag = jsonObject.getString("tagIcon");
        JSONArray options = jsonObject.getJSONArray("options");
        System.out.println("===options==="+options);

        if ("upload".equals(tag)) {
            return question.getScore();
        }

        List<SysQuestionRecord> records = question.getRecords();

        if (records == null || records.size() == 0)
            return 0;

        double score = 0;

        if ("radio".equals(tag)) {
            for (SysQuestionRecord record : records) {
                System.out.println("===record==="+record);
                Integer index = Integer.parseInt(record.getAnswer());
               // JSONObject option = options.getJSONObject();

                JSONObject option=(JSONObject) options.stream().filter(
                        jsonobject->
                                index.equals(((JSONObject)jsonobject).getInteger("value"))

                ).findFirst().get();

                String label = option.getString("label");
                if ("否".equals(label) || "无".equals(label)) {
                    score += 0;
                    continue;
                }

                if ("是".equals(label) || label.indexOf("全部") != -1) {
                    score += question.getScore();
                    continue;
                }

                score += question.getScore() * 0.5;
            }

            return Math.round(score / records.size() * 100) * 0.01d;
        }

        if ("checkbox".equals(tag)) {
            String first = options.getJSONObject(0).getString("label");
            String last = options.getJSONObject(options.size() - 1).getString("label");

            int total = options.size();

            boolean isNagitive= false;

            if ("否".equals(first) || first.indexOf("未") != -1) {
                isNagitive=true;
                total--;
            }
            if ("其他".equals(last) || "其它".equals(last)) {
                total--;
            }

            for (SysQuestionRecord record : records) {
                String anwser = record.getAnswer();
                if(anwser==null||anwser.length()<=2) {
                    score += 0;
                    continue;
                }

                anwser = anwser.substring(1, anwser.length()-1);

                String[] indexs = anwser.split(",");

                if (indexs.length ==1) {
                    Integer index = Integer.parseInt(indexs[0]);
                   // JSONObject option = options.getJSONObject(index);
                    JSONObject option=(JSONObject) options.stream().filter(
                            jsonobject->
                                    index.equals(((JSONObject)jsonobject).getInteger("value"))

                    ).findFirst().get();
                    String label = option.getString("label");
                    if (index==0&&isNagitive) {
                        score += 0;
                        continue;
                    }
                }

                if (indexs.length >= total)
                    score += question.getScore();
                else
                    score += indexs.length / total * question.getScore();
            }
            return Math.round(score / records.size() * 10) * 0.1d;
        }
        return 0;
    }
}
